package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.MemberApplyEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MemberApplyEventListener implements ApplicationListener<MemberApplyEvent> {

    private static final Logger logger = LoggerFactory.getLogger(MemberApplyEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private MemberService memberService;

    @Override
    @Async
    public void onApplicationEvent(MemberApplyEvent event) {
        logger.debug("Member Apply event:{}", "");

        Passenger passenger = (Passenger) event.getSource();

        Setting setting = settingUtils.get();

        //发送短信
//		try {
//			Company memberCompany=companyService.findOne(passenger.getCompanyId());
//			if(StringUtils.isNotBlank(memberCompany.getSignName())){
//				SMSSendUtil.send(passenger.getPhone(), "您的VIP申请已通过【"+memberCompany.getSignName()+"】", settingUtils);
//			}
//		} catch (Exception e) {
//			logger.error("发送短信发生异常：{}",e.getMessage());
//		}

        //给客户推送
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {

                String text = "您的VIP申请已通过";

                //发送设备推送
                PushData pushData = new PushData();
                pushData.setTitle("消息");
                pushData.setType("NEW_NOTICE");
                pushData.setDescription(text);

                GeTuiMember.push(passenger, pushData, setting);
                GeTuiMember.notice(passenger, pushData, setting);

            } catch (IOException e) {
                logger.debug("push error : " + e.getMessage());
            }
        }


    }

}
